<?php
include_once "lib/config.php";
include_once "header.php";

if(isset($_POST['save'])&& $_POST['save']!="")
{
	$data= $_POST['Page'];
	
	if(isset($_POST['page_id']) && $_POST['page_id']>0)
	{
        DB::updateData("pages",$data,array("id"=>$_POST['page_id']));
			
		if(DB::$affectedrows>0)
		{
			$_SESSION['msg_success']="Page updated successfully.";
            redirect('pagemanager.php');
		}
		else
		{
			$msg_error="There is no record updated.";
		}
	}	
}

if(isset($_POST['page_id']) && $_POST['page_id']>0)
{
	$conditionarr=array(":id"=>$_POST['page_id']);
    $pageinfo=DB::fetch_array(DB::query("select * from pages where id=:id",$conditionarr));
    $data=$pageinfo[0];
}
else
{
	redirect('pagemanager.php');
}
?>
<script src="Classes/ckeditor/ckeditor.js"></script>
		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateeditpage();" enctype="multipart/form-data">
			<br>
			<div class="grid_12" >
		    	<?php
            	if(isset($msg_success) && $msg_success!="")
            	{
            	?>
                <div>
                    <span class="notification n-success"><?php echo $msg_success; ?></span>
                </div>
                <?php
                }
                if(isset($msg_error) && $msg_error!="")
                {
                ?>
                <span class="notification n-error"><?php echo $msg_error; ?></span>
                <?php
				}
                ?>
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Page Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
	                  	<p>
	                        <label>Page Name</label>
	                        <input type="text" class="input-short" style="width: 280px;" readonly disabled="true" value="<?php if(isset($data['page_name']) && $data['page_name']!=Null)echo $data['page_name']; ?>"/>
	                    </p>
	                    <p>
	                        <label>Contents<span style="color: red;">*</span></label>
	                        <textarea name="Page[content]" id="page_content" ><?php if(isset($data['content']) && $data['content']!=Null)echo $data['content']; ?></textarea>
	                    </p>
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="page_id" name="page_id" value="<?php if(isset($_POST['page_id'])){ echo $_POST['page_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='pagemanager.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>
CKEDITOR.replace( 'page_content' );
function validateeditpage()
{
	if(trim(document.getElementById("page_content").value)=="")
	{
		alertify.alert("Content field required!");
		document.getElementById("page_content").focus();
		return false;
	}
}
</script>       